package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserLoginVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired(required = false)
    UserMapper userMapper;
    @PostMapping("/reg")
    @ApiOperation("注册用户")
    @ApiOperationSupport(order = 10)
    public JsonResult regUser(@RequestBody @Validated UserRegParam userRegParam){
        log.debug("userRegParam={}",userRegParam);
        if(userMapper.selectUserByUsername(userRegParam.getUsername())>0){
            return new  JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user=new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("user={}",user);
        userMapper.insertUser(user);
        return JsonResult.ok();
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam,
                            @ApiIgnore HttpSession session){
        log.debug("userLoginParam={}",userLoginParam);
        UserLoginVo userLoginVo=userMapper.selectUserByUsernameAndPassword(userLoginParam.getUsername(),userLoginParam.getPassword());
        log.debug("userLoginVo={}",userLoginVo);
        if(userLoginVo==null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userLoginVo);
        return JsonResult.ok(userLoginVo);
    }

    @GetMapping("/currentUser")
    @ApiOperation("获取当前登录用户信息")
    @ApiOperationSupport(order = 30)
    public JsonResult CurrentUser(@ApiIgnore HttpSession session){
        log.debug("session={}",session);
        UserLoginVo userLoginVo=(UserLoginVo)session.getAttribute("user");
        /*if(userLoginVo==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/
        return JsonResult.ok(userLoginVo);
    }
    @GetMapping("/logout")
    @ApiOperation("用户退出")
    @ApiOperationSupport(order = 40)
    public void logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
    }

}
